package com.softserveinc.interactive_board.web;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.softserveinc.interactive_board.domain.User;

import com.softserveinc.interactive_board.service.UserService;
import com.softserveinc.interactive_board.validators.form.EditUserForm;

@Controller
@RequestMapping("/profile")
public class ProfileController {
	private static final String PROFILE_FILE_LOCATION = "profile/index";

	private static final String PROFILE_USER_ATTR_NAME = "userAttr";
	private static final String NEED_EDIT_ATTR_NAME = "edit";

	@RequestMapping(method = RequestMethod.GET)
	public String def() {
		return "redirect:/profile/";
	}

	@RequestMapping("/")
	public String def(Model model) {
		String name = SecurityContextHolder.getContext().getAuthentication()
				.getName();
		if (name.equals("_anonymous"))
			return "redirect:/";
		User user = this.userService.getUser(name);
		model.addAttribute(ProfileController.PROFILE_USER_ATTR_NAME, user);
		return PROFILE_FILE_LOCATION;
	}

	/*
	 * @RequestMapping(method = RequestMethod.GET, value = "/edit") public
	 * String edit(Model model) { String name =
	 * SecurityContextHolder.getContext().getAuthentication() .getName(); if
	 * (!name.equals("anonym")) { User user =
	 * this.interactiveBoardService.getUser(name);
	 * model.addAttribute(PROFILE_FORM_ATTR_NAME, new EditUserForm(user));
	 * model.addAttribute(NEED_EDIT_ATTR_NAME, true); return
	 * PROFILE_FILE_LOCATION; } return "redirect:/registration/"; }
	 * 
	 * @RequestMapping(method = RequestMethod.POST, value = "/edit") public
	 * String edit(Model model, @Valid final EditUserForm form, final
	 * BindingResult result) { if (result.hasErrors()) return
	 * PROFILE_FILE_LOCATION;
	 * this.interactiveBoardService.updateUser(form.getUser()); return
	 * PROFILE_FILE_LOCATION; }
	 */

	private UserService userService;

	@Autowired
	@Qualifier("userServiceImpl")
	public void setUserService(
			UserService userService) {
		this.userService = userService;
	}
}
